﻿package rd.hardcore.ui.pad 
{
	import aze.motion.eaze;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import rd.hardcore.events.SaveManagerEvent;
	import rd.hardcore.params.GlobalParams;
	
	/**
	 * Demande autorisation pour écraser les données déjà existante dans le Pad
	 * @author Thibault Perret
	 */
	public class PadOverridePanel extends Sprite
	{
		
		private var btOk:Sprite = new Sprite();
		private var btCancel:Sprite = new Sprite();
		private var tf:TextField;
		
		public function PadOverridePanel() 
		{
			graphics.beginFill(0x330033, .2);
			graphics.drawRect(0, 0, GlobalParams.PAD_SIZE, GlobalParams.PAD_SIZE);
			graphics.endFill();
			
			btOk.graphics.beginFill(0xffff00);
			btOk.graphics.drawRect(0, 0, 10, 10);
			btOk.graphics.endFill();
			
			btCancel.graphics.beginFill(0xff00ff);
			btCancel.graphics.drawRect(0, 0, 10, 10);
			btCancel.graphics.endFill();
			
			tf = createLabel();
			
			addChild(tf);
			addChild(btOk);
			addChild(btCancel);
			
			btOk.y = btCancel.y = 35;
			btOk.x = 15;
			btCancel.x = 55;
			
			btOk.buttonMode = btCancel.buttonMode = true;
			
			btOk.addEventListener(MouseEvent.CLICK, handleClick);
			btOk.addEventListener(MouseEvent.MOUSE_OVER, handleOut);
			btOk.addEventListener(MouseEvent.MOUSE_OVER, handleOver);
			btCancel.addEventListener(MouseEvent.CLICK, handleClick);
			btCancel.addEventListener(MouseEvent.MOUSE_OVER, handleOut);
			btCancel.addEventListener(MouseEvent.MOUSE_OVER, handleOver);
			
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		
		
		private function handleClick(e:MouseEvent):void 
		{
			switch(e.currentTarget)
			{
				case btOk:
					dispatchEvent(new SaveManagerEvent(SaveManagerEvent.ACCEPT_OVERRIDE));
				break; 
				case btCancel:
					dispatchEvent(new SaveManagerEvent(SaveManagerEvent.CANCEL_OVERRIDE));
				break;
			}
		}
		
		private function handleOver(e:MouseEvent):void 
		{
			eaze(e.currentTarget).to(.3, { alpha:.8 } );
		}
		
		private function handleOut(e:MouseEvent):void 
		{
			eaze(e.currentTarget).to(.3, { alpha:1 }, false );
		}
		
		private function createLabel():TextField
		{
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = "Vous allez écraser des fichiers, êtes vous sûr?";
			
			return tf;
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			btOk.buttonMode = btCancel.buttonMode = false;
			
			btOk.removeEventListener(MouseEvent.CLICK, handleClick);
			btOk.removeEventListener(MouseEvent.MOUSE_OVER, handleOut);
			btOk.removeEventListener(MouseEvent.MOUSE_OVER, handleOver);
			btCancel.removeEventListener(MouseEvent.CLICK, handleClick);
			btCancel.removeEventListener(MouseEvent.MOUSE_OVER, handleOut);
			btCancel.removeEventListener(MouseEvent.MOUSE_OVER, handleOver);
			
			removeChild(tf);
			removeChild(btOk);
			removeChild(btCancel);
		}
	}

}